export const PROGRESS_INFO = {
  content: ['内容', '#F3CE99'],
  language: ['语言', '#DE858E'],
  organization: ['组织', '#FFFE92']
}

export const INFO_MAP = {
  errorWords: {
    type: 'errorWords',
    title: '语法错误',
    titleColor: '#FF4040',
    itemKey: 'wrong_words',
    backupKey: 'error_type_level_one',
    highlightColor: '#EDC4C8',
    structureName: 'errorWordsStruct'
  },
  suggestionWord: {
    type: 'suggestionWord',
    title: '建议',
    titleColor: '#30C9A1',
    itemKey: 'word',
    highlightColor: '#AEC48E',
    structureName: 'suggestionWordStruct'
  },
  conjunctionList: {
    type: 'conjunctionList',
    title: '连词',
    titleColor: '#F8AC23',
    itemKey: 'word',
    highlightColor: '#F0C592',
    structureName: 'conjunctionListStruct'
  },
  clauseList: {
    type: 'clauseList',
    title: '从句',
    titleColor: '#E99D43',
    itemKey: 'contentType',
    highlightColor: '#F0C592',
    structureName: 'clauseListStruct'
  },
  phrasesList: {
    type: 'phrasesList',
    title: '词组',
    titleColor: '#E99D43',
    itemKey: 'sentence',
    highlightColor: '#F0C592',
    structureName: 'phrasesListStruct'
  }
}

// raw data
// 建议列表
export const getProposalListData = (data) => {
  const { suggestion } = data
  const gecSuggestion = suggestion
    .reduce((obj, item) => {
      if (!obj[item.word_info.sentence_id]) {
        obj[item.word_info.sentence_id] = []
      }
      obj[item.word_info.sentence_id].push(item)
      return obj
    }, [])
    .filter((item) => !!item)
  gecSuggestion.forEach((item, key) => {
    gecSuggestion[key] = gecSuggestion[key].sort(
      (a, b) => a.word_info.start_pos - b.word_info.start_pos
    )
  })

  return gecSuggestion.flat()
}

// 连词列表
export const getConjunctionList = (data) => {
  const {
    conjunction_list: conjunctionList,
    conjunctions_descriptions: conjunctionsDescriptions
  } = data.writing_highlight.conjunctions_info
  return conjunctionList
    .map((item) => {
      return [
        ...item.conjunctions.map((val) => {
          return {
            ...val,
            sentence_id: item.sentence_id,
            contentType: conjunctionsDescriptions[val.type],
            content: val.word,
            ending: '让句子衔接很顺畅'
          }
        })
      ]
    })
    .flat()
}

// 词组列表
export const getPhrasesList = (data) => {
  const { phrases } = data.writing_highlight
  const phrasesArr = []
  const gecSuggestion = phrases
    .reduce((obj, item) => {
      if (!obj[item.sentence_id]) {
        obj[item.sentence_id] = []
      }
      obj[item.sentence_id].push(item)
      return obj
    }, [])
    .filter((item) => !!item)
  gecSuggestion.forEach((item) => {
    let startPos = 0
    item.forEach((val) => {
      if (startPos <= val.start_pos) {
        phrasesArr.push({
          ...val,
          contentType: '词组',
          content: val.sentence,
          ending: '使用的很专业'
        })
      }
      startPos = val.end_pos
    })
  })
  return phrasesArr
}

// 从句列表
export const getClauseList = (data) => {
  const clauseType = {
    attributive_clause: '定语从句',
    object_clause: '宾语从句',
    adverbial_clause: '状语从句'
  }
  const {
    writing_highlight: { clause_info },
    original_essay_info: { original_sentence_list }
  } = data
  const list = []
  Object.keys(clause_info).forEach((i) => {
    if (i !== 'path_id') {
      const item = clause_info[i]
      item.info.forEach((val) => {
        list.push({
          ...val,
          contentType: clauseType[i],
          content: original_sentence_list[val.sentence_id].original_text,
          ending: '使用的很巧妙，是推荐的表达方式'
        })
      })
    }
  })
  return list.sort((item, cur) => item.sentence_id - cur.sentence_id)
}

// structure

// 语法错误结构
export const getError = (data) => {
  const {
    gec_result: gecResult,
    original_essay_info: { original_sentence_list }
  } = data
  let number = 1
  const gecResultObj = gecResult.reduce((obj, item) => {
    obj[item.sentence_id] = item.error_info
    return obj
  }, {})
  const arr = original_sentence_list.map((item) => {
    const { original_text: originalText, sentence_id } = item
    const text = []
    let startPos = 0
    const error_info = gecResultObj[sentence_id]
      ? gecResultObj[sentence_id]
      : []
    error_info.forEach((errorInfo) => {
      const { start_pos, end_pos } = errorInfo
      text.push({ text: originalText.slice(startPos, start_pos) })
      text.push({
        text: originalText.slice(start_pos, end_pos),
        isBorder: true,
        index: number
      })
      number += 1
      startPos = end_pos
    })
    if (startPos !== originalText.length) {
      text.push({ text: originalText.slice(startPos) })
    }
    return { ...item, originalText, text }
  })

  return arr.reduce((arrTotal, item) => {
    if (!arrTotal[item.paragraph_id]) {
      arrTotal[item.paragraph_id] = []
    }
    arrTotal[item.paragraph_id].push(...item.text)
    return arrTotal
  }, [])
}

// 建议结构
export const getProposalData = (data) => {
  const {
    suggestion,
    original_essay_info: { original_sentence_list }
  } = data
  let number = 1
  const gecSuggestion = suggestion.reduce((obj, item) => {
    if (!obj[item.word_info.sentence_id]) {
      obj[item.word_info.sentence_id] = []
    }
    obj[item.word_info.sentence_id].push(item)
    return obj
  }, {})
  const arr = original_sentence_list.map((item) => {
    const { original_text: originalText, sentence_id } = item
    const text = []
    let startPos = 0
    const suggestionInfo = gecSuggestion[sentence_id]
      ? gecSuggestion[sentence_id].sort(
        (a, b) => a.word_info.start_pos - b.word_info.start_pos
      )
      : []
    suggestionInfo.forEach((suggestionInfoItem) => {
      const { start_pos, end_pos } = suggestionInfoItem.word_info
      text.push({ text: originalText.slice(startPos, start_pos) })
      text.push({
        text: originalText.slice(start_pos, end_pos),
        isBorder: true,
        index: number
      })
      number += 1
      startPos = end_pos
    })
    if (startPos !== originalText.length) {
      text.push({ text: originalText.slice(startPos) })
    }
    return { ...item, originalText, text }
  })
  return arr.reduce((arrTotal, item) => {
    if (!arrTotal[item.paragraph_id]) {
      arrTotal[item.paragraph_id] = []
    }
    arrTotal[item.paragraph_id].push(...item.text)
    return arrTotal
  }, [])
}

// 连词结构
export const getConjunctionArticle = (data) => {
  const {
    original_essay_info: { original_sentence_list },
    writing_highlight: { conjunctions_info }
  } = data
  let number = 1
  const gecResultObj = conjunctions_info.conjunction_list.reduce(
    (obj, item) => {
      obj[item.sentence_id] = item.conjunctions
      return obj
    },
    {}
  )
  const arr = original_sentence_list.map((item) => {
    const { original_text: originalText, sentence_id } = item
    const text = []
    let startPos = 0
    const suggestionInfo = gecResultObj[sentence_id]
      ? gecResultObj[sentence_id]
      : []
    suggestionInfo.forEach((value) => {
      const { start_pos, end_pos } = value
      text.push({ text: originalText.slice(startPos, start_pos) })
      text.push({
        text: originalText.slice(start_pos, end_pos),
        isBorder: true,
        index: number,
        start_pos,
        end_pos
      })
      number += 1
      startPos = end_pos
    })
    if (startPos !== originalText.length) {
      text.push({ text: originalText.slice(startPos) })
    }
    return { ...item, originalText, text }
  })
  return arr.reduce((arrTotal, item) => {
    if (!arrTotal[item.paragraph_id]) {
      arrTotal[item.paragraph_id] = []
    }
    arrTotal[item.paragraph_id].push(...item.text)
    return arrTotal
  }, [])
}

// 词组结构
export const getPhrasesArticle = (data) => {
  const {
    original_essay_info: { original_sentence_list },
    writing_highlight: { phrases }
  } = data
  let number = 1
  const arr = original_sentence_list.map((item) => {
    const { original_text: originalText, sentence_id } = item
    const text = []
    let startPos = 0
    const phrasesList = phrases.filter(
      (val) => val.sentence_id === sentence_id
    )
    phrasesList.forEach((value) => {
      const { start_pos, end_pos } = value
      if (startPos <= start_pos) {
        text.push({ text: originalText.slice(startPos, start_pos) })
        text.push({
          text: originalText.slice(start_pos, end_pos),
          isBorder: true,
          index: number,
          start_pos,
          end_pos
        })
        number += 1
        startPos = end_pos
      }
    })
    if (startPos !== originalText.length) {
      text.push({ text: originalText.slice(startPos) })
    }
    return { ...item, originalText, text }
  })
  return arr.reduce((arrTotal, item) => {
    if (!arrTotal[item.paragraph_id]) {
      arrTotal[item.paragraph_id] = []
    }
    arrTotal[item.paragraph_id].push(...item.text)
    return arrTotal
  }, [])
}

// 从句结构
/* eslint-disable no-plusplus */
export const getClauseArticle = (data) => {
  const {
    writing_highlight: { clause_info },
    original_essay_info: { original_sentence_list }
  } = data
  const obj = {}
  let number = 1
  Object.keys(clause_info).forEach((i) => {
    if (i !== 'path_id') {
      const { info } = clause_info[i]
      info.reduce((totalObj, item) => {
        totalObj[item.sentence_id] = item.sentence_id
        return totalObj
      }, obj)
    }
  })
  const arr = original_sentence_list.map((item) => {
    const { original_text: originalText, sentence_id } = item
    const text = []
    text.push({
      text: originalText,
      ...(obj[sentence_id] !== undefined
        ? { isBorder: true, index: number++ }
        : {})
    })
    return { ...item, originalText, text }
  })
  return arr.reduce((arrTotal, item) => {
    if (!arrTotal[item.paragraph_id]) {
      arrTotal[item.paragraph_id] = []
    }
    arrTotal[item.paragraph_id].push(...item.text)
    return arrTotal
  }, [])
}
